package com.cheetah.start.common.plugin.log;

import cn.hutool.core.util.StrUtil;
import com.cheetah.start.module.monitor.domain.SysLog;
import com.cheetah.start.module.monitor.service.SysLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

/**
 * @author xjh
 * @create 2021/7/5
 **/
@Slf4j
@Component
public class LogFactory {
    @Autowired
    private SysLogService sysLogService;

    @Async
    public void record(SysLog sysLog) {
        String requestBody = sysLog.getRequestBody();
        String requestParam = sysLog.getRequestParam();
        if (StrUtil.isNotBlank(requestParam) && requestParam.length() > 255) {
            sysLog.setRequestParam(requestParam.substring(0, 255));
        }
        if (StrUtil.isNotBlank(requestBody) && requestBody.length() > 255) {
            sysLog.setRequestBody(requestBody.substring(0, 255));
        }
        sysLogService.save(sysLog);
    }
}
